package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public Result getAllIteam() {
        List<CheckItem> allIteam = checkItemService.getAllIteam();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, allIteam);
    }

    /**
     * 根据id查
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findItemById(Integer id) {
        CheckItem itemById = checkItemService.findItemById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, itemById);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = checkItemService.findPage(queryPageBean);
        if (page != null) {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, page);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    /**
     * 新增项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result addItem(@RequestBody CheckItem checkItem) {
        Integer row = checkItemService.addItem(checkItem);
        if (row != null && row == 1){
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        }
        return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
    }

    /**
     * 修改项
     * @param checkItem
     * @return
     */
    @PutMapping("/edit")
    public Result updateItem(@RequestBody CheckItem checkItem){
        Integer rows = checkItemService.updateItem(checkItem);
        System.out.println("rows" + rows);
        if (rows != null && rows == 1){
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }

        return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
    }


    /**
     * 根据id删除项
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id){
        Integer rows = checkItemService.deleteById(id);
        if (rows != null && rows == 1){
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }
        return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
    }

}
